import cors from 'koa2-cors';
import compress from 'koa-compress';
import bodyParser from 'koa-bodyparser';
import range from 'koa-range';
import koaBody from 'koa-body';
import path from 'path';
import resourcesProxy from './proxy/resourcesProxy';
import errHandler from './lib/errHandler';
import appFileRoutes from '../routes/AppFileRoutes';
import ProxyRoutes from '../routes/ProxyRoutes';
import DBRoutes from '../routes/DBRoutes';
import AuthorizeRoutes from '../routes/AuthorizeRoutes';
import configTool from '../com/configTool';
import logHandler from './lib/logHandler';
import IPAuthorityHandler from './lib/IPAuthorityHandler';
import mapServices from '../routes/MapServicesRoutes';
import TileRoutes from '../routes/TileRoutes';
import plugsRoutes from '../../plug/routes/plugsRoutes';
import plugsMiddlewares from '../../plug/middleware/plugsMiddlewares';
import TokenAuthorityHandler from './lib/TokenAuthorityHandler';
import ManaRoutes from '../routes/ManaRoutes';
import WindowsInfo from '../routes/WindowsInfo';
import DocRoutes from '../routes/DocRoutes';
import responseSISFormat from './lib/responseSISFormat';

const {koaSwagger} = require('koa2-swagger-ui');


function clientMiddleWare(app: any) {

    // 允许跨域请求
    configTool.baseConfig.clientCORS && app.use(cors());

    const baseConfig = configTool.baseConfig;
    app.use(
        koaSwagger({
            routePrefix: '/doc', // 自定义 koa2-swagger-ui 生成的交互界面 url
            hideTopbar: false,
            favicon: '/favicon.ico', // default favicon
            swaggerOptions: {
                // 重要选项！url地址一定要指向为 swagger-jsdoc 配置的路由地址
                // url: `http://${baseConfig.localIPAddress}:${baseConfig.proxyAfterProxy_Port}/Web/API/api_doc.json`,
                url: `http://${baseConfig.localIPAddress}:${baseConfig.proxyAfterProxy_Port}/Doc/api`
            }
        })
    );

    // app.use(koaBody({
    //     multipart: true,
    //     formLimit: '2000mb',
    //     jsonLimit: '20mb',
    //     formidable: {
    //         keepExtensions: true, // 保留文件扩展名
    //         maxFileSize: 8 * 1024 * 1024 * 1024,
    //         uploadDir: path.join(configTool.wwwPath, '/Web/SimpleEarth/temp'),  // 临时目录，但不会保存文件
    //         onFileBegin: (name, file) => {
    //             console.log(name, file);
    //             //    console.log('fileBegin', name, file);
    //             const buf = fs.readFileSync(file.filepath);
    //             console.log(buf);
    //             // 不保存文件到本地，只处理为 Buffer
    //             // @ts-ignore
    //             file.path = '';
    //         }
    //     }
    // }));


    app.use(bodyParser({
        formLimit: '20mb',
        jsonLimit: '20mb'
    }));


    app.use(compress({br: false}));
    app.use(responseSISFormat());
    app.use(errHandler());
    app.use(logHandler());
    app.use(IPAuthorityHandler(['*'], configTool.baseConfig.blackList));



    app.use(range);


    // 数据转发缓存服务
    app.use(ProxyRoutes.routes());
    app.use(mapServices.routes());
    app.use(appFileRoutes.routes());
    app.use(DBRoutes.routes());
    app.use(TileRoutes.routes());
    app.use(AuthorizeRoutes.routes());

    app.use(TokenAuthorityHandler());
    resourcesProxy(app, configTool.wwwPath);

    app.use(ManaRoutes.routes());
    app.use(WindowsInfo.routes());
    app.use(DocRoutes.routes());

    for (let i = 0; i < plugsMiddlewares.length; i++) {
        app.use(plugsMiddlewares[i]());
    }
    for (let i = 0; i < plugsRoutes.length; i++) {
        app.use(plugsRoutes[i].routes());
    }


}

export default clientMiddleWare;
